package com.yf1706.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yf1706.bean.CheckAccount;
import com.yf1706.bean.CheckAffairApply;
import com.yf1706.bean.CheckDetail;
import com.yf1706.bean.CheckMonthDetail;
import com.yf1706.bean.CheckMonthReport;
import com.yf1706.bean.CheckRule;
import com.yf1706.bean.CheckWithcard;
import com.yf1706.bean.Emp;
import com.yf1706.service.CheckAccountService;
import com.yf1706.service.CheckDetailsService;
import com.yf1706.service.CheckMonthReportService;
import com.yf1706.service.CheckWithCardService;
import com.yf1706.vo.CheckAccountVO;
import com.yf1706.vo.CheckUPAccountVO;
import com.yf1706.vo.CheckUPWithCardVO;
import com.yf1706.vo.CheckWithCardVO;

@Controller
@RequestMapping("card")
public class CheckFlushCardController extends BaseController{
	@Autowired
	private CheckDetailsService cds;
	@Autowired
	private CheckWithCardService cwcs;
	@Autowired
	private CheckAccountService cas;
	@Autowired
	private CheckMonthReportService cmrs;
	//查询全部刷卡信息
	@RequestMapping("getcardDetails")
	@ResponseBody
	public Map<String, Object> getcardDetails(int page,int rows){
		 Page<CheckDetail> pages = PageHelper.startPage(page, rows);
         List<CheckDetail> list= cds.getCheckDetailsList();
         Map<String, Object> map = new HashMap<String, Object>();
         long total = pages.getTotal();
		 map.put("total", total);
		 map.put("rows", list); 
		 return map;
	}
	
	//新增外部刷卡数据的导入信息
		@RequestMapping("addCheckcard")
		@ResponseBody
		public void addCheckcards(HttpServletRequest request, HttpServletResponse response){
			 String jsondata=request.getParameter("jsondata");
			 System.out.println(jsondata);
			 List<CheckDetail> list = JSONObject.parseArray(jsondata,CheckDetail.class);
			for (CheckDetail checkDetail : list) {
				cds.addCheckcards(checkDetail);
			}
			 
	         PrintWriter out;
				try {
					out = response.getWriter();
					out.print("ok");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
		}
	//模糊查询全部刷卡信息
	@RequestMapping("serchCheckDetails")
	@ResponseBody
	public List<CheckDetail> serchCheckDetails(String value){
		if(value.equals("")) {
			 int id=-1;
	         List<CheckDetail> list= cds.serchCheckDetailsList(id);
			 return list;
		}else {
		 int id=Integer.parseInt(value);
         List<CheckDetail> list= cds.serchCheckDetailsList(id);
		 return list;
		 }
	}
	//删除申请信息
	@RequestMapping("deleteCheckDetails")
	@ResponseBody
	public void deleteCheckDetails(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
         cds.deleteCheckDetail(list.get(0).intValue());
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//查询全部配卡信息
	@RequestMapping("getCheckWithCard")
	@ResponseBody
	public Map<String, Object> getCheckWithCards(int page,int rows){
		 Page<CheckWithcard> pages = PageHelper.startPage(page, rows);
         List<CheckWithcard> list= cwcs.CheckWithcardList();
         Map<String, Object> map = new HashMap<String, Object>();
         long total = pages.getTotal();
		 map.put("total", total);
		 map.put("rows", list); 
		 return map;
	}
	//模糊查询全部配卡信息
	@RequestMapping("serchCheckWithCard")
	@ResponseBody
	public List<CheckWithcard> serchCheckWithCards(String value){
		int empId=Integer.parseInt(value);
         List<CheckWithcard> list= cwcs.getSerchCheckWithcardList(empId);
		 return list;
	}
	//删除申请信息
	@RequestMapping("deleteCheckWithCard")
	@ResponseBody
	public void deleteCheckWithCards(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
		 cwcs.deleteWithCard(list.get(0).intValue());
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//新增配卡信息
	@RequestMapping("addCheckWithCard")
	@ResponseBody
	public void addCheckWithCards(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 List<CheckWithCardVO> list = JSONObject.parseArray(jsondata,CheckWithCardVO.class);
		 list.get(0).setMaker(getSessionUserName(request));
		 cwcs.addCheckWithcard(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//修改考勤配卡信息
	@RequestMapping("updateCheckWithCard")
	@ResponseBody
	public void updateCheckRules(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("update_jsondata");
		 List<CheckUPWithCardVO> list = JSONObject.parseArray(jsondata, CheckUPWithCardVO.class);
		 list.get(0).setUpdateEmp(getSessionUserName(request));
		 cwcs.updateCheckWithcard(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	

	//查询全部考勤账套信息
	@RequestMapping("getCheckAccount")
	@ResponseBody
	public Map<String, Object> getCheckAccounts(int page,int rows){
		 Page<CheckAccount> pages = PageHelper.startPage(page, rows);
         List<CheckAccount> list= cas.CheckAccountList();
         Map<String, Object> map = new HashMap<String, Object>();
         long total = pages.getTotal();
		 map.put("total", total);
		 map.put("rows", list); 
		 return map;
	}
	//查询全部考勤账套信息
	@RequestMapping("getCheckAccounts")
	@ResponseBody
	public List<CheckAccount> getCheckAccounts(){
         List<CheckAccount> list= cas.CheckAccountList();
		 return list;
	}
	//模糊查询全部考勤账套信息
	@RequestMapping("serchCheckAccount")
	@ResponseBody
	public List<CheckAccount> serchCheckAccounts(String value){
		if(value.equals("")) {
			int accountId=-1;	
			 List<CheckAccount> list= cas.getSerchCheckAccountList(accountId);
			 return list;	
		}else {
			int accountId=Integer.parseInt(value);
	         List<CheckAccount> list= cas.getSerchCheckAccountList(accountId);
			 return list;	
		}
		
	}
	//删除考勤账套信息
	@RequestMapping("deleteCheckAccount")
	@ResponseBody
	public void deleteCheckAccounts(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("huo_rowData");
		 List<Integer> list = (List<Integer>) JSONObject.parse(jsondata);	
		 cas.deleteCheckAccount(list.get(0).intValue());
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//新增考勤账套信息
	@RequestMapping("addCheckAcccount")
	@ResponseBody
	public void addCheckAcccounts(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("jsondata");
		 System.out.println(jsondata);
		 List<CheckAccountVO> list = JSONObject.parseArray(jsondata,CheckAccountVO.class);
		 list.get(0).setMaker(getSessionUserName(request));
		 cas.addCheckAccount(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	//修改考勤账套信息
	@RequestMapping("updateCheckAccount")
	@ResponseBody
	public void updateCheckAccounts(HttpServletRequest request, HttpServletResponse response){
		 String jsondata=request.getParameter("update_jsondata");
		 List<CheckUPAccountVO> list = JSONObject.parseArray(jsondata, CheckUPAccountVO.class);
		 list.get(0).setUpdateEmp(getSessionUserName(request));
		 cas.updateCheckAccount(list.get(0));
         PrintWriter out;
			try {
				out = response.getWriter();
				out.print("ok");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	//查询全部配卡信息
		@RequestMapping("getCheckReport")
		@ResponseBody
		public Map<String, Object> getCheckReports(int page,int rows){
			 Page<CheckMonthReport> pages = PageHelper.startPage(page, rows);
	         List<CheckMonthReport> list= cmrs.queryCheckMonthReports();
	         Map<String, Object> map = new HashMap<String, Object>();
	         long total = pages.getTotal();
			 map.put("total", total);
			 map.put("rows", list); 
			 return map;
		}
		//模糊查询某个员工的月报表信息
		@RequestMapping("serchCheckMonthReport")
		@ResponseBody
		public List<CheckMonthReport> serchCheckAccounts(String begin,String empId){
            if(empId.equals("")) {
            	 int empId1=-1;
    	         List<CheckMonthReport> list= cmrs.serchCheckMonthReports(begin, empId1);
    			 return list;
            }else {
			 int empId1=Integer.parseInt(empId);
	         List<CheckMonthReport> list= cmrs.serchCheckMonthReports(begin, empId1);
			 return list;
            }
		}
}
